<?php
/**
 * @desc：服务商会员接口管理类
 * @auth:liuqingyan(814418144@qq.com)
 * $id:Suser.php 2022-05-18 17:57:00
 */
namespace app\api\controller;

use app\api\service\User as UserServer;
use app\api\validate\Certification;
use app\common\controller\Api;
use app\common\library\Auth;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\UserProfile;
use fast\Random;
use think\Exception;
use think\Log;
use think\Validate;
class Suser extends Api
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','loginByWxCode'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @desc:会员中心
     */
    public function index()
    {
        $userInfo = (new \app\api\service\User())->getUserInfo($this->auth);
        $this->success('', $userInfo);
    }

    /**
     * @desc:会员登录
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->request('account');
        $password = $this->request->request('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = (new UserServer())->getUserInfo($this->auth);
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @desc:手机验证码登录
     * @auth:liuqingyan(814418177@qq.com) 2022-05-20 18:29:00 
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = $this->request->request('mobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            if ($user->status != 'normal') {
                $this->error(__('Account is locked'));
            }
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    
    /**
     * @desc:注册会员
     * @auth:liuqingyan(814418177@qq.com) 2022-05-23 09:29:00 
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $email    邮箱
     * @param string $mobile   手机号
     * @param string $code   验证码
     */
    public function register()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        $email = $this->request->request('email');
        $mobile = $this->request->request('mobile');
        $code = $this->request->request('code');
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        if ($email && !Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $ret = Sms::check($mobile, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $ret = $this->auth->register($username, $password, $email, $mobile, []);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @desc:退出登录
     * @auth:liuqingyan(814418177@qq.com) 2022-05-30 10:23
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * @desc:修改会员个人信息
     * @auth:liuqingyan(814418177@qq.com) 2022-05-30 10:55
     */
    public function profile()
    {
        if (!$this->request->isPost()){
            $this->error("请求方式异常");
        }
        $user = $this->auth->getUser();
        $nickname = $this->request->post('nickName');
        $gender = $this->request->post('gender');
        $country = $this->request->post('country');
        $province = $this->request->post('province');
        $introduction = $this->request->post('introduction');
        $birthday = $this->request->post('birthday');
        $city = $this->request->post('city');
        $get_wechat_profile = $this->request->post('get_wechat_profile');
        $avatar = $this->request->post('avatarUrl', '', 'trim,strip_tags,htmlspecialchars');
        $mobile = $this->request->post('mobile');

        if ($nickname) {
            $user->nickname = $nickname;
        }

        if ((!empty($gender) || $gender === '0') && in_array($gender, array('0','1'))) {
            $user->gender = $gender;
        }
        if ($avatar) {
            $user->avatar = $avatar;
        }
        if ($country) {
            $user->country = $country;
        }
        if ($province) {
            $user->province = $province;
        }
        if ($city) {
            $user->city = $city;
        }
        if ($introduction) {
            $user->introduction = $introduction;
        }
        if ($birthday) {
            try {
                $user->birthday = date('Y-m-d', strtotime($birthday));
            }catch (Exception $e){
            }
        }
        if ($get_wechat_profile) {
            $user->get_wechat_profile = $get_wechat_profile;
        }

        $user->save();
        $this->success('修改成功');
    }

    /**
     * @desc:修改企业信息
     * @auth:liuqingyan(814418177@qq.com) 2022-06-07 10:55
     */
    public function enterprise()
    {
        if (!$this->request->isPost()){
            $this->error("请求方式异常");
        }
        $data = input('param.');
        $user = $this->auth->getUser();

        if (!$user->enterprise || $user->enterprise->certification_status != 2){
            $this->error('请先进行企业认证或等待企业认证完成');
        }
        if (isset($data['contact_info'])) {
            $user->enterprise->contact_info = $data['contact_info'];
        }
        if (isset($data['company_profile'])) {
            $user->enterprise->company_profile = $data['company_profile'];
        }
        if (isset($data['welfare'])) {
            $user->enterprise->welfare = $data['welfare'];
        }
        if (isset($data['latitude_longitude'])) {
            $user->enterprise->latitude_longitude = $data['latitude_longitude'];
        }
        if (isset($data['photos'])) {
            $user->enterprise->photos = json_encode($data['photos']);
        }
        $user->enterprise->save();
        $this->success('修改成功');
    }
}
